package com.lastcrdg.spiderwebview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/9/30 14:03
 * description：
 */

public class SpiderView extends View {

    private int width;
    private int height;
    //蜘蛛网画笔
    private Paint spiderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    //覆盖层画笔
    private Paint upPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    //原点画笔
    private Paint pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public SpiderView(Context context) {
        super(context);
    }

    public SpiderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public SpiderView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint() {
        spiderPaint.setColor(Color.BLACK);
        spiderPaint.setStyle(Paint.Style.STROKE);
        spiderPaint.setStrokeWidth(2);
        upPaint.setColor(0xcc7D7DFC);
        upPaint.setStyle(Paint.Style.FILL);
        pointPaint.setColor(Color.BLUE);
        pointPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = MeasureSpec.getSize(widthMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(width / 2, height / 2);
        canvas.drawColor(Color.WHITE);
        drawSpider(canvas);
        drawUP(canvas);
    }

    private int maxRadius = 400;//最大半径
    private int cout = 5;//蜘蛛网条数
    private int numRect = 6;//确定是几边形

    private void drawSpider(Canvas canvas) {
        Path path = new Path();
        float angel = (float) (2 * Math.PI / numRect);
        int r = maxRadius / cout; //蜘蛛网之间的间隔
        //画蜘蛛网
        for (int i = 1; i <= cout; i++) {
            path.reset();
            int curR = r * i;
            path.moveTo(curR, 0);
            for (int j = 1; j < numRect; j++) {
                float currentX = (float) (curR * Math.cos(angel * j));
                float currentY = (float) (curR * Math.sin(angel * j));
                path.lineTo(currentX, currentY);
            }
            path.close();
            canvas.drawPath(path, spiderPaint);
        }
        //画三条斜线
        for (int i = 1; i <= numRect; i++) {
            path.reset();
            path.moveTo(0, 0);
            float topX = (float) (maxRadius * Math.cos(angel * i));
            float topY = (float) (maxRadius * Math.sin(angel * i));
            path.lineTo(topX, topY);
            canvas.drawPath(path, spiderPaint);
        }
    }

    int[] data = {50, 80, 70, 100, 20, 100};
    int sumData = 100;

    private void drawUP(Canvas canvas) {
        Path path = new Path();
        Path bluePath = new Path();
        float angel = (float) (2 * Math.PI / numRect);
        for (int i = 1; i <= numRect; i++) {
            float percent = (float) data[i-1] / (float)sumData;
            float pointX = (float) (maxRadius * percent * Math.cos(angel * i));
            float pointY = (float) (maxRadius * percent * Math.sin(angel * i));
            if (i == 1) {
                bluePath.moveTo(pointX, pointY);
            }else {
                bluePath.lineTo(pointX, pointY);
            }
            path.addCircle(pointX, pointY, 10, Path.Direction.CW);
        }
        canvas.drawPath(path, pointPaint);
        bluePath.close();
        canvas.drawPath(bluePath, upPaint);
    }
}
